package com.xbongbong.saas.help;

import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.pojo.CallReckonPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.service.toolbox.statistic.help.ChartQueryHelp;
import com.xbongbong.pro.service.toolbox.statistic.help.SystemChartHelp;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.pro.statistic.result.pojo.TotalAndAggPojo;
import com.xbongbong.saas.enums.CallLogResultEnum;
import com.xbongbong.saas.enums.CallLogTypeEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 呼叫统计帮助类
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/6/26 15:43
 * @since v1.0
 */
@Component
public class CallReckonHelp {
    @Resource
    private ChartQueryHelp chartQueryHelp;
    @Resource
    private SystemChartHelp systemChartHelp;
    @Resource
    private EsHelper esHelper;

    /**
     * 今日呼出时长，呼出数
     *
     * @param callReckonPojo 入参
     * @return com.xbongbong.pro.statistic.result.pojo.TotalAndAggPojo
     * @author 徐俊杰
     * @date 2019/6/27 10:23
     * @since v1.0
     */
    public TotalAndAggPojo callDuration(CallReckonPojo callReckonPojo) throws XbbException {
        //时间和类型
        todayOutbound(callReckonPojo);
        //query
        BoolQueryBuilder callQuery = chartQueryHelp.callReckonQuery(callReckonPojo);
        //求和字段
        String sumField = "duration";
        //统计
        return systemChartHelp.getInt4Total(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG, callQuery, sumField);
    }

    /**
     * 今日呼出成功数
     *
     * @param callReckonPojo 入参
     * @return int 呼出成功数
     * @author 徐俊杰
     * @date 2019/6/27 10:24
     * @since v1.0
     */
    public int callSuccessCount(CallReckonPojo callReckonPojo) throws XbbException {
        //时间和类型
        todayOutbound(callReckonPojo);
        callReckonPojo.setResult(CallLogResultEnum.SUCCESS.getCode());
        //query
        BoolQueryBuilder callQuery = chartQueryHelp.callSuccessQuery(callReckonPojo);
        //统计
        ElasticAggsPojo elasticAggsPojo = esHelper.elasticAggs(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG, callQuery, null);
        String total = elasticAggsPojo.getTotal();
        return StringUtil.toInt(total);
    }

    /**
     * 今日，呼出
     *
     * @param callReckonPojo 参数容器
     * @author 徐俊杰
     * @date 2019/6/27 10:18
     * @since v1.0
     */
    private void todayOutbound(CallReckonPojo callReckonPojo) {
        //时间
        int startTime = DateUtil.getTodayInt();
        int endTime = startTime + TimeConstant.SECONDS_PER_DAY;
        callReckonPojo.setTimeAttr("addTime");
        callReckonPojo.setStartTime(startTime);
        callReckonPojo.setEndTime(endTime);
        //类型
        callReckonPojo.setType(CallLogTypeEnum.OUTBOUND.getCode());
    }
}
